<template>
	<view class="container">
		<view class="formBox">
			<uni-forms ref="form" :modelValue="form">
				<view class="box">
					<uni-forms-item required label="姓名" name="customer_name">
						<uni-easyinput type="text" v-model="form.customer_name" :clearable="false" placeholder="请输入姓名" />
					</uni-forms-item>
					<uni-forms-item required label="手机号" name="customer_mobile">
						<uni-easyinput type="number" v-model="form.customer_mobile" :clearable="false" placeholder="请输入联系方式" />
					</uni-forms-item>
					<uni-forms-item label="证件号" name="customer_idcard">
						<uni-easyinput type="idcard" v-model="form.customer_idcard" :clearable="false" placeholder="请输入证件号" />
					</uni-forms-item>
				</view>
				<view class="box">
					<uni-forms-item required label="开始时间" name="start_date">
						<picker mode="date" @change="bindTimeStart" :value="form.start_date">
							<view class="picker-input">
								{{ form.start_date }}
								<uni-icons type="right" size="16" color="#bbb"></uni-icons>
							</view>
						</picker>
					</uni-forms-item>
					<uni-forms-item required label="结束时间" name="end_date">
						<picker mode="date" @change="bindTimeEnd" :value="form.end_date">
							<view class="picker-input">
								{{ form.end_date }}
								<uni-icons type="right" size="16" color="#bbb"></uni-icons>
							</view>
						</picker>
					</uni-forms-item>
				</view>
				<view class="btns">
					<button class="btn" type="primary" @click="submit">确定</button>
				</view>
			</uni-forms>
		</view>
	</view>
</template>

<script>
import api from '@/api/api';
import { isPhone } from '@/utils/validate';
export default {
	data() {
		return {
			sn: null,
			type: 'rent',
			form: {},
			rules: {
				customer_mobile: {
					rules: [
						{
							required: true,
							errorMessage: '请填写手机号'
						},
						{
							validateFunction: function (rule, value, data, callback) {
								if (!isPhone(value)) {
									callback('请输入正确的手机号');
								}
								return true;
							}
						}
					],
					validateTrigger: 'submit'
				},
				customer_name: {
					rules: [
						{
							required: true,
							errorMessage: '请填写姓名'
						}
					],
					validateTrigger: 'submit'
				}
			}
		};
	},
	onLoad(e) {
		this.sn = e.sn;
		this.type = e.type;
	},
	onShow() {
		this.getDetail();
	},
	onReady() {
		this.$refs.form.setRules(this.rules);
		if (this.type == 'rent') {
			uni.setNavigationBarTitle({
				title: '入住信息'
			});
		} else if (this.type == 'book') {
			uni.setNavigationBarTitle({
				title: '预定信息'
			});
		}
	},
	methods: {
		bindTimeStart(e) {
			this.form.start_date = e.detail.value;
		},
		bindTimeEnd(e) {
			this.form.end_date = e.detail.value;
		},
		submit() {
			this.$refs.form.validate().then((res) => {
				api.editCustomer(this.form).then((res) => {
					if (res.data.is_success) {
						uni.showToast({
							title: '编辑成功'
						});
						setTimeout(() => {
							uni.hideToast();
							uni.navigateBack();
						}, 1000);
					} else {
						uni.showToast({
							title: '无信息更改',
							icon: 'none'
						});
					}
				});
			});
		},
		getDetail() {
			api.rentDetail({ rent_sn: this.sn }).then((res) => {
				this.form = res.data.info.customer_info;
				this.form.customer_id = res.data.info.customer_info.id;
				this.form.rent_sn = res.data.info.rent_sn;
				this.form.start_date = res.data.info.start_date;
				this.form.end_date = res.data.info.end_date;
			});
		}
	}
};
</script>